package com.mangowang.module.main.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseFragment;
import com.mangowang.lib.common.help.UserHelper;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Merchant;
import com.mangowang.lib.common.proto.Mystock;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ShopNewFragment extends BaseFragment {

    Unbinder unbinder;

    @BindView(R2.id.ll_shop_bg)
    LinearLayout llShopBg;
    @BindView(R2.id.tv_total)
    TextView tvTotal;
    @BindView(R2.id.tv_ji_huo)
    TextView tvJiHuo;
    @BindView(R2.id.tv_shop_one)
    TextView tv_shop_one;
    @BindView(R2.id.tv_shop_two)
    TextView tv_shop_two;
    @BindView(R2.id.tv_shop_three)
    TextView tv_shop_three;
    @BindView(R2.id.tv_shop_four)
    TextView tv_shop_four;
    @BindView(R2.id.tv_shop_five)
    TextView tv_shop_five;
    @BindView(R2.id.tv_shop_six)
    TextView tv_shop_six;
    @BindView(R2.id.tv_shop_seven)
    TextView tv_shop_seven;


    @BindView(R2.id.rv_shop_one)
    RelativeLayout rv_shop_one;

    @BindView(R2.id.rv_shop_two)
    RelativeLayout rv_shop_two;

    @BindView(R2.id.rv_shop_three)
    RelativeLayout rv_shop_three;

    @BindView(R2.id.rv_shop_four)
    RelativeLayout rv_shop_four;

    @BindView(R2.id.rv_shop_five)
    RelativeLayout rv_shop_five;

    @BindView(R2.id.rv_shop_six)
    RelativeLayout rv_shop_six;

    @BindView(R2.id.rv_shop_seven)
    RelativeLayout rv_shop_seven;



    private int position;  //
    private String name;  //


    private String type;

    public static ShopNewFragment NewInstance(int instanceType,String name) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        bundle.putString("name",name);
        ShopNewFragment messageTypeFragment = new ShopNewFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
        name = getArguments().getString("name");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        if(position % 2 ==1) llShopBg.setBackgroundResource(R.mipmap.shop_bg_1);
        if(position % 2 == 0) llShopBg.setBackgroundResource(R.mipmap.shop_bg_2);
        loadData();
        rv_shop_one.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ShopNewNextActivity.class);
            intent.putExtra("type","1");
            intent.putExtra("pattern",String.valueOf(position));
            intent.putExtra("title",name + "全部商户");
            startActivity(intent);
        });
        rv_shop_two.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ShopNewNextActivity.class);
            intent.putExtra("type","2");
            intent.putExtra("pattern",String.valueOf(position));
            intent.putExtra("title",name + "未激活商户");
            startActivity(intent);
        });
        rv_shop_three.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ShopNewNextActivity.class);
            intent.putExtra("type","3");
            intent.putExtra("pattern",String.valueOf(position));
            intent.putExtra("title",name + "新增商户");
            startActivity(intent);
        });
        rv_shop_four.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ShopNewNextActivity.class);
            intent.putExtra("type","4");
            intent.putExtra("pattern",String.valueOf(position));
            intent.putExtra("title",name + "优质商户");
            startActivity(intent);
        });
        rv_shop_five.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ShopNewNextActivity.class);
            intent.putExtra("type","5");
            intent.putExtra("pattern",String.valueOf(position));
            intent.putExtra("title",name + "活跃商户");
            startActivity(intent);
        });
        rv_shop_six.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ShopNewNextActivity.class);
            intent.putExtra("type","6");
            intent.putExtra("pattern",String.valueOf(position));
            intent.putExtra("title",name + "非活跃商户");
            startActivity(intent);
        });
        rv_shop_seven.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ShopNewNextActivity.class);
            intent.putExtra("type","7");
            intent.putExtra("pattern",String.valueOf(position));
            intent.putExtra("title",name + "沉默商户");
            startActivity(intent);
        });
    }

    /**
     * 加载数据
     */
    public void loadData() {

        Merchant.merchant.Builder builder = Merchant.merchant.newBuilder();
        builder.setPattern(String.valueOf(position));
        NetworkManager.INSTANCE.post(Apis.merchant, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Merchant.merchant bean = Merchant.merchant.parseFrom(response);
                Log.e("bean",bean.toString());
               tvTotal.setText(bean.getTrading());
               tvJiHuo.setText(bean.getActivation());
               tv_shop_one.setText(bean.getTotalNum());
               tv_shop_two.setText(bean.getUnactivationNum());
               tv_shop_three.setText(bean.getNewlyNum());
               tv_shop_four.setText(bean.getHighQualityNum());
               tv_shop_five.setText(bean.getLivelyNum());
               tv_shop_six.setText(bean.getUnlivelyNum());
               tv_shop_seven.setText(bean.getSilentNum());


            }
            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });

    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
